package gapidt.plugin.actions;

import gapidt.plugin.AnalysisResultsManager;

import org.apache.log4j.Logger;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;


public class AnalyzeFromEditor implements IEditorActionDelegate {
	
	static Logger logger = Logger.getLogger(AnalyzeFromEditor.class);
	
	public static final String ID = AnalyzeFromEditor.class.getCanonicalName(); 

	IEditorPart editor = null;
	
	@Override
	public void run(IAction action) {
		if(editor != null){
			//There is an open Java editor
			
			//Obtener el Compilation Unit del archivo Java abierto
			IEditorInput editorInput = editor.getEditorInput();

			ICompilationUnit iCompilationUnit = JavaUI.getWorkingCopyManager().getWorkingCopy(editorInput);
			
			if(iCompilationUnit.exists()){
				AnalysisResultsManager.getInstance().clearResults();
				AnalysisResultsManager.getInstance().analyzeElement(iCompilationUnit, editor);
				
			}else
				logger.debug("Compilation Unit does not exists");
			
		}else
			//No open editor
			logger.debug("There are no open Java Editors");
		
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		
	}

	@Override
	public void setActiveEditor(IAction action, IEditorPart currentEditor) {
		if(currentEditor != null)
			editor = currentEditor;
	}
}
